//
//  HJVideoPlayController.m
//  KaiChat
//
//  Created by lx on 2024/3/25.
//

#import "TJVideoPlayController.h"
//#import "ZFIJKPlayerManager.h"
#import "ZFAVPlayerManager.h"

#import "TJPlayerControlView.h"
#import "ZFPlayerConst.h"
#import "ZFUtilities.h"
#import "UIView+ZFFrame.h"


@interface TJVideoPlayController ()

@property (nonatomic, strong) ZFPlayerController *player;
@property (nonatomic, strong) UIImageView *containerView;
@property (nonatomic, strong) TJPlayerControlView *controlView;

@end

@implementation TJVideoPlayController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationBar.alpha = 0;
    [self addBackButton];
    self.view.backgroundColor = UIColor.blackColor;
    [self.view addSubview:self.containerView];
    [self setupPlayer];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.player.viewControllerDisappear = NO;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.player.viewControllerDisappear = YES;
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
 
    self.containerView.frame = CGRectMake(0, KStatusBarHight, self.view.bounds.size.width, self.view.bounds.size.height - KStatusBarHight - TabbarSafeBottomMargin);
}

- (void)setupPlayer {
    ZFAVPlayerManager *playerManager = [[ZFAVPlayerManager alloc] init];
    playerManager.shouldAutoPlay = YES;
    
    /// 播放器相关
    self.player = [ZFPlayerController playerWithPlayerManager:playerManager containerView:self.containerView];
    self.player.controlView = self.controlView;
    /// 设置退到后台继续播放
    self.player.pauseWhenAppResignActive = NO;
//    self.player.resumePlayRecord = YES;
    
    @zf_weakify(self)
    self.player.orientationDidChanged = ^(ZFPlayerController * _Nonnull player, BOOL isFullScreen) {
        /* // 使用YYTextView转屏失败
        for (UIWindow *window in [UIApplication sharedApplication].windows) {
            if ([window isKindOfClass:NSClassFromString(@"YYTextEffectWindow")]) {
                window.hidden = isFullScreen;
            }
        }
        */
    };
    /// 播放完成
    TJWeakify(self)
    self.player.playerDidToEnd = ^(id  _Nonnull asset) {
        [weakself.player seekToTime:0 completionHandler:^(BOOL finished) {
            [weakself.controlView.portraitControlView playBtnSelectedState:NO];
        }];
    };
    
    if (_localPath && [[NSFileManager defaultManager] fileExistsAtPath:_localPath]) {
        NSLog(@"播放本地视频 %@", _localPath);
        self.player.assetURL = [NSURL fileURLWithPath:_localPath];
    } else {
        if (!isEmptyString(_url)) {
            NSLog(@"加载网络视频 %@", _url);
            self.player.assetURL = _url.URL;
        } else {
            [TJHUD showMessage:@"视频url错误" inView:self.view];
            return;
        }
    }
    
    self.player.currentPlayerManager.view.coverImageView.image = self.coverImage;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

- (BOOL)prefersStatusBarHidden {
    return NO;
}

- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
    return UIStatusBarAnimationNone;
}

- (BOOL)shouldAutorotate {
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (UIModalPresentationStyle)modalPresentationStyle {
    return UIModalPresentationFullScreen;
}

- (TJPlayerControlView *)controlView {
    if (!_controlView) {
        _controlView = [TJPlayerControlView new];
        _controlView.fastViewAnimated = YES;
        _controlView.autoHiddenTimeInterval = 5;
        _controlView.autoFadeTimeInterval = 0.5;
        _controlView.prepareShowLoading = YES;
        _controlView.prepareShowControlView = NO;
        _controlView.showCustomStatusBar = YES;
        _controlView.effectViewShow = NO;
    }
    return _controlView;
}

- (void)popViewController {
    [super popViewController];
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (UIImageView *)containerView {
    if (!_containerView) {
        _containerView = [UIImageView new];
        _containerView.image = self.coverImage;
        _containerView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _containerView;
}

- (void)dealloc {
    NSLog(@"%s", __func__);
}
 
@end
